/// Creator:      gaoruishan
/// CreatDate:    2025-06-04
/// Description:  医嘱单相关
Class Nur.MNISV3.Service.OrderSheet Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// w ##class(Nur.MNISV3.Service.OrderSheet).GetOrderSheetV4("144","2","8","20665")
ClassMethod GetOrderSheetV4(episodeId = "", hospitalId, locId = "", userId = "")
{
    n (episodeId,hospitalId, locId,userId)
    s $zt="errGetOrderSheetV4"
    s ^GRS("GetOrderSheetV4")=$lb(episodeId,hospitalId, locId,userId)
    s Msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s patInfoList=##class(%ListOfDataTypes).%New()
    s sheetItems= ..GetSheetItems(hospitalId,locId)
    
    s screenParts=##class(%ListOfDataTypes).%New()
    s part1=..AddScreenPartObject("true","priority","优先级","Single","全部!长期!临时")
    d screenParts.Insert(part1)
    s part1=..AddScreenPartObject("true","status","状态","Single","全部!有效!新开!停止")
    d screenParts.Insert(part1)
    s part1=..AddScreenPartObject("true","category","类别","Single","全部!药品!检验!其他")
    d screenParts.Insert(part1)
    
    s patinfo=##class(%ArrayOfDataTypes).%New()
   
    s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
    s orderSheet=##class(%ListOfDataTypes).%New()
  
    k sortArr,ordIdArr
    f i=1:1:sheetItems.Count() {
        s sheetItem=sheetItems.GetAt(i)
        s mark=sheetItem.GetAt("mark")
        s name=sheetItem.GetAt("name")
        s getOrders=##class(Nur.NIS.Service.OrderSheet.Sheet).getOrders(mark,episodeId,userId,locId,0,"")
        s pages=getOrders.GetAt("pages")
        f pageInd=1:1:pages.Count() {
            s page=pages.GetAt(pageInd)
            s locNo=page.GetAt("LocNo")
            s orders=page.GetAt("orders")
            s newPages=##class(%ArrayOfDataTypes).%New()
            d newPages.SetAt(locNo,"LocNo")
            d newPages.SetAt(mark,"mark")
            s newOrders=##class(%ListOfDataTypes).%New()
            f orderInd=1:1:orders.Count() {
                s order=orders.GetAt(orderInd)
                s ordId=order.GetAt("ID")
                s oeordId=+ordId
                s oeordSub=$p(ordId,"||",2)
                s qyxflag=0,ordtypedesc="",ordType="",qbgflag=1 //判断标记
                s orderStartDate=$p(^OEORD(oeordId,"I",oeordSub,1),"^",9)  
                s orderStartTime=$p(^OEORD(oeordId,"I",oeordSub,1),"^",10)
                s startDate=$zd(orderStartDate,3)
                s startTime=$zt(orderStartTime,2)
                s stopDoctorStr=##class(Nur.DoctorOrderSheet).getStopDoctor(oeordId,oeordSub)
                s stopDate=$p(stopDoctorStr,"^",2)
                i stopDate'="" d
                .s stopDate=$TR($E(stopDate,1,10),"-","/")
                .s stopDate=$p(stopDate,"/",1)_"-"_$p(stopDate,"/",2)_"-"_$p(stopDate,"/",3)
                s curdate=+$h
                i stopDate="" s qyxflag=1
                //w stopDate,!
                i (stopDate'="")&&($zdh(stopDate,3)>curdate) s qyxflag=1
                i (stopDate'="")&&($zdh(stopDate,3)=curdate) s qbgflag=0
                i $zdh(startDate,3)=curdate s qbgflag=0
                i (qyxflag=1) s ordStatus="有效"
                i (qbgflag=0) s ordStatus="新开"
                i (stopDate'="")  s ordStatus="停止" 
                s children=order.GetAt("children")
                i (children="")||(children.Count()=0) {
                    i $g(ordIdArr(ordId))'="" { //取出主医嘱
                        s order1=ordIdArr(ordId)
                        s children1=order1.GetAt("children")
                        d children1.Insert(order)
                    }else {   
                        s screenPart="priority|全部!"_name_"^status|全部!"_ordStatus_"^category|全部"
                        d order.SetAt(screenPart,"screenParts")
                        d newOrders.Insert(order)
                    }
                }else {
                    d order.SetAt(##class(%ListOfDataTypes).%New(),"children")
                    f j=1:1:children.Count(){ //记录子医嘱
                        s temId=children.GetAt(j)
                        s ordIdArr(temId)=order
                    }
                    s screenPart="priority|全部!"_name_"^status|全部!"_ordStatus_"^category|全部"
                    d order.SetAt(screenPart,"screenParts")
                    d newOrders.Insert(order)
                }
            }
            d newPages.SetAt(newOrders,"orders")
            d orderSheet.Insert(newPages)
        }
    }     
    d patinfo.SetAt(orderSheet,"orderSheet")
    d patInfoList.Insert(patinfo)  
     
    d json.SetAt($g(Msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(patInfoList),"patients")
    d json.SetAt($g(sheetItems),"sheetItems")
    d json.SetAt($g(screenParts),"screenParts")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)

errGetOrderSheetV4
    s Msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(Msg,"100021","-1")
}

/// w ##class(Nur.MNISV3.Service.OrderSheet).GetSheetItems(2,15)
ClassMethod GetSheetItems(hospId, locId) As %String
{
    
    n (hospId,locId,%session)
    s items=##class(%ListOfDataTypes).%New()
    s list=##class(Nur.NIS.Service.OrderSheet.Sheet).getSheets(hospId,locId)
    f i=1:1:list.Count(){
        s obj=list.GetAt(i)
        s mark=obj.GetAt("mark")
        s columns=##class(Nur.NIS.Service.OrderSheet.Sheet).getSheetConfig(hospId,mark)
        d obj.SetAt($g(ID),"ID")
        d obj.SetAt($g(sortby),"sortby")
        d obj.SetAt($g(columns),"columns")
        d items.Insert(obj)
      
    }
    //w ##class(Nur.JSON).Encode(items),!   
    q items
}

ClassMethod AddScreenPartObject(commonKey, keyCode, keyDesc, keyType, keyValue)
{
    s obj=##class(%ArrayOfDataTypes).%New()
    d obj.SetAt($g(commonKey),"commonKey")
    d obj.SetAt($g(keyCode),"keyCode")
    d obj.SetAt($g(keyDesc),"keyDesc")
    d obj.SetAt($g(keyType),"keyType")
    d obj.SetAt($g(keyValue),"keyValue")
    q obj
}

ClassMethod GetMainOrdId(oeoreId) As %String
{
     q $p($g(^OEORD(+oeoreId,"I",$p(oeoreId,"||",2),11)),"^",39)
}

ClassMethod GetMainOreId(oeoreId) As %String
{
    n (oeoreId)
    s oew=+oeoreId,chl=$p(oeoreId,"||",2),sub=$p(oeoreId,"||",3)
    i (sub'="") s mainOreId=$p($g(^OEORD(oew,"I",chl,"X",sub,"BILL")),"^",3)
    e  s mainOreId=$p($g(^OEORD(oew,"I",chl,11)),"^",39)
    q mainOreId
}

}
